package com.example.administrator.boltshow;

import android.content.Intent;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.google.gson.Gson;

import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

import java.util.ArrayList;
import java.util.List;

import adapter.BrandsDetailAdapter;
import config.NetConfig;
import decoration.ItemDecoration;
import model.BrandDetailModel;

public class BrandDetailActivity extends AppCompatActivity {

    private String brandName;
    private String brandId;
    private ImageView ivBack;
    private TextView tvTitle;
    private ActionBar mActionBar;
    private RecyclerView mRecyclerView;
    private BrandsDetailAdapter mAdapter;
    private  int CurrentPage=1;
    private List<BrandDetailModel.DataBean> mData;
    private GridLayoutManager manager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_brand_detail);
        Intent intent = getIntent();
        brandName = intent.getStringExtra("brandName");
        brandId = intent.getStringExtra("brandId");
        mActionBar = getSupportActionBar();
        mActionBar.hide();
        initView();
        initData();
        setAdapter();
        setListener();

    }

    private void setAdapter() {
        manager = new GridLayoutManager(this,2,GridLayoutManager.VERTICAL,false);
        mRecyclerView.setLayoutManager(manager);
        mRecyclerView.setAdapter(mAdapter);


    }

    private void initData() {
        
        mData=new ArrayList<>();
        mAdapter=new BrandsDetailAdapter(this,mData);

        loadMore(1);
        mRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
            }

            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);
                if(newState==RecyclerView.SCROLL_STATE_IDLE) {
                    int itemPosition = manager.findLastVisibleItemPosition();
                    if(itemPosition==mData.size()-1) {
                        CurrentPage++;
                        loadMore(CurrentPage);
                    }

                }

            }
        });
        
    }

    private void loadMore(int currentPage) {
        String format = String.format(NetConfig.PRAISE_LIST, Integer.parseInt(brandId), currentPage);
        x.http().get(new RequestParams(format), new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                  if(result!=null) {
                      Gson gson=new Gson();
                      BrandDetailModel detailModel = gson.fromJson(result, BrandDetailModel.class);
                      List<BrandDetailModel.DataBean> data = detailModel.getData();
                      mData.addAll(data);
                  }
                mAdapter.notifyDataSetChanged();
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {

            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {

            }
        });
    }

    private void setListener() {
        ivBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
         tvTitle.setText(brandName);
        mAdapter.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
               Integer position= (Integer) v.getTag();

                Intent intent=new Intent(BrandDetailActivity.this,ShopActivity.class);
                Bundle bundle=new Bundle();
                bundle.putString("linkid",brandId);
                bundle.putString("goodsid",mData.get(position).getGoodsId());
                intent.putExtra("bundle",bundle);
                startActivity(intent);

            }
        });


    }

    private void initView() {
        ivBack = (ImageView) findViewById(R.id.iv_brandDetail_back);
        tvTitle = (TextView) findViewById(R.id.tv_brandDetail_title);
        mRecyclerView= (RecyclerView) findViewById(R.id.rv_branddetail_show);
        mRecyclerView.addItemDecoration(new ItemDecoration(10));
    }
    
    


}
